package com.smartfarm.service;

import com.smartfarm.domain.equipment.AgriculturalEquipment;
import com.smartfarm.domain.equipment.EquipmentStatus;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备管理器
 */
public class EquipmentManager {
    private final Map<String, AgriculturalEquipment> equipment = new HashMap<>();
    private final Map<String, EquipmentStatus> equipmentStatus = new HashMap<>();

    public void registerEquipment(AgriculturalEquipment device) {
        equipment.put(device.getId(), device);
        // 初始化设备状态
        EquipmentStatus status = EquipmentStatus.builder()
            .equipmentId(device.getId())
            .fuelLevel(100.0)
            .speed(0.0)
            .workProgress(0.0)
            .healthStatus("Good")
            .operationMode("Auto")
            .currentTask("None")
            .build();
        equipmentStatus.put(device.getId(), status);
        System.out.println("Registered equipment: " + device.getName());
    }

    public void updateEquipmentStatus(String equipmentId, EquipmentStatus status) {
        if (equipment.containsKey(equipmentId)) {
            equipmentStatus.put(equipmentId, status);
            System.out.println("Updated status for: " + equipment.get(equipmentId).getName());
            System.out.println(status);
        }
    }

    public EquipmentStatus getEquipmentStatus(String equipmentId) {
        return equipmentStatus.get(equipmentId);
    }

    public List<AgriculturalEquipment> getAllEquipment() {
        return new ArrayList<>(equipment.values());
    }

    public AgriculturalEquipment getEquipment(String equipmentId) {
        return equipment.get(equipmentId);
    }

    public void removeEquipment(String equipmentId) {
        AgriculturalEquipment device = equipment.remove(equipmentId);
        if (device != null) {
            equipmentStatus.remove(equipmentId);
            System.out.println("Removed equipment: " + device.getName());
        }
    }

    public void updateEquipmentLocation(String equipmentId, double latitude, double longitude) {
        EquipmentStatus status = equipmentStatus.get(equipmentId);
        if (status != null) {
            status.setLatitude(latitude);
            status.setLongitude(longitude);
            System.out.println("Updated location for " + equipment.get(equipmentId).getName() + 
                " to (" + latitude + ", " + longitude + ")");
        }
    }

    public void updateEquipmentHealth(String equipmentId, String healthStatus) {
        EquipmentStatus status = equipmentStatus.get(equipmentId);
        if (status != null) {
            status.setHealthStatus(healthStatus);
            System.out.println("Updated health status for " + equipment.get(equipmentId).getName() + 
                " to: " + healthStatus);
        }
    }
} 